<template>
  <div class="register">
    <h2>团队互评系统</h2>
    <div class="register-wrap">
      <el-form :label-position="labelPosition" label-width="120px" :model="formData">
          <el-form-item label="用户邮箱">
            <el-input v-model="formData.email"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="formData.password" show-password></el-input>
          </el-form-item>
          <el-form-item label="请再次输入密码">
            <el-input v-model="formData.password1" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="registerHandler">提交</el-button>
          </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import api from '../api/index'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
export default {
  setup () {
    const router = useRouter()
    const formData = ref({
      email: '',
      name: '',
      password: '',
      password1: ''
    })
    const registerHandler = async () => {
      if (formData.value.password !== formData.value.password1) {
        ElMessage.error("两次输入的密码不一致")
        return
      }
      const res = await api.register({ ...formData.value })
      if (res.data.code === 200) {
        ElMessage.success({
          message: '注册成功',
          type: 'success'
        })
        router.push('/login')
      } else {
        ElMessage.error(res.data.msg)
      }
    }
    return {
      formData,
      registerHandler
    }
  }
}
</script>

<style lang="scss" scoped>
  .register{
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    h2{
      position: fixed;
      left: 30px;
      top: 20px;
    }
    .register-wrap{
      width: 400px;
      height: 450px;
      padding: 15px;
      border: 1px solid rgb(21, 86, 184);
      border-radius: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
</style>
